package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fs.entity.Administrators;
import com.fs.entity.Materials;
import com.fs.entity.ResponseData;
import com.fs.mapper.AdministratorsMapper;
import com.fs.mapper.MaterialsMapper;
import com.fs.service.IAdministratorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@Service
public class AdministratorsServiceImpl implements IAdministratorsService {
    @Autowired
    AdministratorsMapper administratorsMapper;
    @Autowired
    MaterialsMapper materialsMapper;
    @Override
    public ResponseData Login(String acount, String apassword) {
        QueryWrapper<Administrators> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("acount",acount);
        Administrators administrators=administratorsMapper.selectOne(queryWrapper);
        if (administrators==null){
            return ResponseData.error("用户不存在");
        }else if (administrators.getApassword().equals(apassword)){

            return ResponseData.ok(administrators);
        }
        return ResponseData.error("密码错误");
    }

    @Override
    public ResponseData Update(String newapassword, String newaphnumber, String newemail,int aid) {
        UpdateWrapper<Administrators> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("aid",aid);
        Administrators administratorForUpdate = new Administrators();
        administratorForUpdate.setApassword(newapassword); // 设置新密码
        administratorForUpdate.setAphnumber(newaphnumber); // 设置新电话号码
        administratorForUpdate.setEmail(newemail); // 设置新电子邮件
        int updateResult = administratorsMapper.update(administratorForUpdate,updateWrapper);
        if (updateResult>0){
            return ResponseData.ok(200,"修改成功");
        }
        return ResponseData.error("修改失败");
    }

    @Override
    public ResponseData Spotcheck() {
        QueryWrapper<Materials> queryWrapper=new QueryWrapper<>();
        queryWrapper.gt("state",2);
        List<Materials> list=materialsMapper.selectList(queryWrapper);
        int size =list.size();
        Random random=new Random();
        int number=random.nextInt(size);
        System.out.println(list.get(number));
        return ResponseData.ok(list.get(number));
    }
}
